//
//  SIXLoginController.m
//  discover
//
//  Created by 张天兵 on 16/5/22.
//  Copyright © 2016年 张天兵. All rights reserved.
//

#import "SIXLoginController.h"
#import "SIXCover.h"
#import "SIXPopMenu.h"

#define account @"11"
#define password @"11"
#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height

@interface SIXLoginController ()<SIXPopMenuDelegate>
/** 登录界面图片 */
@property (weak, nonatomic) IBOutlet UIImageView *pitureImageView;
/** 账号图标 */
@property (weak, nonatomic) IBOutlet UIImageView *accountImageView;
/** 账号输入框 */
@property (weak, nonatomic) IBOutlet UITextField *accountTF;
/** 密码图标 */
@property (weak, nonatomic) IBOutlet UIImageView *passwordImageView;
/** 密码输入框 */
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;
/** 登录按钮 */
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
/** 登录进度指示 */
@property(nonatomic, strong) UIActivityIndicatorView *indicatorView;

@end

@implementation SIXLoginController

#pragma mark - 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置登录按钮初始状态
    self.loginBtn.enabled = NO;
    
    // 设置输入框监听
    [self.accountTF addTarget:self action:@selector(accountTextChange:) forControlEvents:UIControlEventEditingChanged];
    [self.passwordTF addTarget:self action:@selector(passwordTextChange:) forControlEvents:UIControlEventEditingChanged];
  
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 登录图片圆形化处理
    self.pitureImageView.layer.cornerRadius = self.pitureImageView.bounds.size.width * 0.5;
    self.pitureImageView.layer.masksToBounds = YES;
    
    // 设置leftView
    self.passwordTF.rightViewMode = UITextFieldViewModeUnlessEditing;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [btn sizeToFit];
    btn.titleLabel.font = [UIFont systemFontOfSize:11];
    [btn setTitleColor:[UIColor colorWithRed:49/255.0 green:55/255.0 blue:116/255.0 alpha:1.0] forState:UIControlStateNormal];
    
    self.passwordTF.rightView = btn;
    
    [btn addTarget:self action:@selector(forgetPassword:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark - 监听事件
/**  监听账号输入 */
- (void)accountTextChange:(UITextField *)tf {
    NSLog(@"accountTextChange----");
    // 判断是否更换图标
    if (tf.text.length) {
        self.accountImageView.image = [UIImage imageNamed:@"login_user_highlighted"];
    }else {
        self.accountImageView.image = [UIImage imageNamed:@"login_user"];
    }
    
#warning    /***************  此部分联网时需修改--- begin ************/
    // 注册用户登录图片,服务器端验证账号存在后执行以下代码
    if ([tf.text isEqualToString:account]) {
        self.pitureImageView.image = [UIImage imageNamed:@"login_default"];
    }else {
        self.pitureImageView.image = [UIImage imageNamed:@"login_picture"];
    }
    /***************  此部分联网时需修改--- end ************/
    
    // 登录按钮状态控制
    [self checkLoginState];

}

/**  监听密码输入 */
- (void)passwordTextChange:(UITextField *)tf {
    NSLog(@"passwordTextChange----");
    // 判断是否更换图标
    if (tf.text.length) {
        self.passwordImageView.image = [UIImage imageNamed:@"login_key_highlighted"];
    }else {
        self.passwordImageView.image = [UIImage imageNamed:@"login_key"];
    }
    
    // 登录按钮状态控制
    [self checkLoginState];
    
}

/** 登录按钮状态控制 */
- (void)checkLoginState {
    
    if (self.accountTF.text.length && self.passwordTF.text.length) {
        self.loginBtn.enabled = YES;
    }else {
        self.loginBtn.enabled = NO;
    }

}

/**  登录 */
- (IBAction)loginBtnClick:(UIButton *)sender {
    
    if ([self.accountTF.text isEqualToString:account] && [self.passwordTF.text isEqualToString:password]) {
        
        // 登录指示
        self.indicatorView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 5, 250, 30)];
        [self.loginBtn addSubview:self.indicatorView];
        [self.indicatorView startAnimating];
        [self.loginBtn setTitle:@"登录中..." forState:UIControlStateNormal];
        
#warning        /************ 此部分联网时需修改----begin *************/
        // 账号和密码服务器端验证成功后执行以下代码
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.indicatorView stopAnimating];
            [self.indicatorView removeFromSuperview];
            [self.loginBtn setTitle:@"登录" forState:UIControlStateNormal];
            
            // 1.弹出蒙版
            [SIXCover show];
            
            // 2.弹出pop菜单
            SIXPopMenu *menu = [SIXPopMenu showInPoint:CGPointMake(screenW * 0.5, screenH * 0.4)];
            [menu startPopAnimation];
            menu.delegate = self;
            
            // 3. 关闭键盘
            [self.view endEditing:YES];
            
        });
        /************ 此部分联网时需修改----end *************/
        
    }else {
    
        [self popTips:@"账号或密码错误"];
    }
}

/**  忘记密码 */
- (void)forgetPassword:(UIButton *)btn {

    [self popTips:@"程序猿哥哥吐血中...该功能暂未开放"];
}

 /**  关闭页面 */
- (IBAction)closeBtnClick:(UIButton *)sender {
    NSLog(@"closeBtnClick------");
    // 返回设置界面
#warning    // insert code...
}


/**  无密码登录 */
- (IBAction)noPasswordBtnClick:(UIButton *)sender {
    [self popTips:@"程序猿哥哥吐血中...该功能暂未开放"];
}

/**  注册 */
- (IBAction)registerBtnClick:(UIButton *)sender {
    [self popTips:@"程序猿哥哥吐血中...该功能暂未开放"];
}

/**  QQ登录 */
- (IBAction)qqLoginBtnClick:(UIButton *)sender {
    [self popTips:@"程序猿哥哥吐血中...该功能暂未开放"];
}

#pragma mark - 提示框
- (void)popTips:(NSString *)string {

    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:string message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alertVc addAction:cancelAction];
    [self presentViewController:alertVc animated:YES completion:nil];

}


#pragma mark - SIXPopMenuDelegate
// 点击菜单中取消按钮或验证成功就会调用
- (void)popMenuDidCancelOrSuccess:(SIXPopMenu *)popMenu {
    // 移除蒙版
    [SIXCover hide];
    // 移除菜单
    [SIXPopMenu hide];
}


@end
